package root.electronclass.electronclass.common.util;

import java.text.DecimalFormat;
import java.util.regex.Pattern;

/**
 * 页面：
 *
 * @author zhangguihao
 */
public class TextUtils {
    private static DecimalFormat format = null;

    private static Pattern getPhonePattern() {
        return Holder.phonePattern;
    }

    public static boolean isPhoneNumber(String phone) {
        if (null == phone) {
            return false;
        }
        return getPhonePattern().matcher(phone).matches();


    }

    private static class Holder {
        static Pattern phonePattern = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
    }

    public static String format(double number) {
        if (format == null) {
            format = new DecimalFormat("#.00");
        }
        String numStr = number + "";
        if ((numStr).endsWith("0")) {
            return numStr.substring(0, numStr.length() - 2);
        }
        return format.format(number);
    }


    public static String checkAndGetPicUrl(String url) {
        if (url != null && url.startsWith("http")) {
            return url;
        } else {
            return "https://pics6.baidu.com/feed/00e93901213fb80ebec822a76f7ec525b83894e4.jpeg@f_auto?token=e710cf73e8f8dca318a9d1d15063f7f3";
        }
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        if (strLen == 0) {
            return str;
        }

        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();

                sb.append("0").append(str);//左补0

                // sb.append(str).append("0");//右补0

                str = sb.toString();

                strLen = str.length();

            }
        }
        return str;
    }
}
